/*
 * BBMPlatformDemoContextListener.java
 *
 * Research In Motion Limited proprietary and confidential
 * Copyright Research In Motion Limited, 2011-2011
 */
package com.allkapps.bbmopenchat;


import com.allkapps.bbmopenchat.utils.Worker;

import net.rim.blackberry.api.bbm.platform.BBMPlatformContext;
import net.rim.blackberry.api.bbm.platform.BBMPlatformContextListener;
import net.rim.device.api.system.ApplicationDescriptor;
import net.rim.device.api.system.ApplicationManager;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.Dialog;

/**
 * Listens for changes in the platform context e.g. success, error, or pending states.
 * 
 */
public class BBMOpenChatContextListener extends BBMPlatformContextListener {

    /**
     * Initializes a context listener with the screen that will be updated based on changes in the context.
     * 
     * @param demoApp
     *            non-null demo app that will be updated based on access changes.
     * 
     * @throws NullPointerException
     *             if <code>demoScreen</code> is null.
     */
    public BBMOpenChatContextListener( ) {
    }

    private void handlePending() {
    }

    private void _accessChanged( boolean isAccessAllowed, int accessErrorCode ) {
        /* *LOOK_HERE*
         * Looks at the error code and updates the view accordingly.
         */
        final BBMPlatformContext platformContext = BBMBridge.getInstance().getBBMPlatformContext();
        switch( accessErrorCode ) {
            case BBMPlatformContext.ACCESS_ALLOWED:
                BBMBridge.getInstance().accessAllowed();
                break;
            case BBMPlatformContext.ACCESS_BLOCKED_BY_USER:
                if (!platformContext.requestUserPermission()) {
                    // show the user error
                    Runnable requestUserPermission = new Runnable() {
                        public void run() {
                            platformContext.requestUserPermission();
                        }
                    };
                    Worker.getInstance().addToQueue(requestUserPermission);
                    //show error screen
                }
                break;
            case BBMPlatformContext.ACCESS_REREGISTRATION_REQUIRED:
                // Stop all activities related to BBM Social SDK and restart the app
                handleAppRestart();
                break;
            default:
                handlePending();
                break;
        }
    }

    /* (non-Javadoc)
     * @see net.rim.blackberry.api.bbm.platform.BBMPlatformContextListener#accessChanged(boolean, int)
     */
    public void accessChanged( final boolean isAccessAllowed, final int accessStatusCode ) {
        // run in background to make UI responsive
        new Thread( new Runnable() {
            public void run() {
                _accessChanged( isAccessAllowed, accessStatusCode );
            }
        } ).start();
    }

    private void handleAppRestart() {
        ApplicationDescriptor current = ApplicationDescriptor.currentApplicationDescriptor();
        UiApplication.getUiApplication().invokeAndWait( new Runnable() {
            public void run() {
                Dialog.inform( "You application needs to be restarted for changes to take effect.");
            }
        } );
        // Schedules are rounded to the nearest minute so there could be a bit of delay in restart.
        ApplicationManager.getApplicationManager().scheduleApplication( current, System.currentTimeMillis(), true );
        System.exit( 0 );

    }

}
